home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / music_utilities / pt144.dms / pt144.adf / MIDI_Playground-1.03 / SourceCode / mp.h < prev    next >
C/C++ Source or Header  |  1993-01-23  |  9KB  |  234 lines

  1. /**************************************************************************
  2. * mp.h:        Main header file.
  3. *        Part of MP, the MIDI Playground.
  4. *
  5. * Author:    Daniel Barrett
  6. * Version:    See the file "version.h".
  7. * Copyright:    None!  This program is in the Public Domain.
  8. *        Please share it with others.
  9. ***************************************************************************/
  10.  
  11.     
  12. #ifndef _MP_H
  13.  
  14. #include <devices/serial.h>
  15. #include <exec/devices.h>
  16. #include <exec/memory.h>
  17. #include <exec/ports.h>
  18. #include <exec/types.h>
  19. #include <exec/interrupts.h>
  20. #include <libraries/dos.h>
  21. #include <libraries/dosextens.h>
  22. #include <workbench/startup.h>
  23. #include <workbench/workbench.h>
  24. #include <functions.h>
  25. #include <stdio.h>
  26.  
  27. #ifndef __C_MACROS__
  28. #define __C_MACROS__
  29. #endif
  30. #include <ctype.h>
  31.  
  32.  
  33. /**************************************************************************
  34. * Types.
  35. **************************************************************************/
  36.     
  37. typedef unsigned char    STATE_T;    /* An automaton state.        */
  38. typedef UBYTE        MIDI_VALUE;    /* A single MIDI data value.    */
  39. typedef    signed char    MIDI_RESULT;    /* What happened while reading?    */
  40. typedef char        FLAGS;        /* Internal flags.        */
  41.     
  42. /**************************************************************************
  43. * Return values from our input routine.
  44. **************************************************************************/
  45.     
  46. #define    RESULT_OK        (MIDI_RESULT)(1)    /* Legal value read.   */
  47. #define    RESULT_ERROR        (MIDI_RESULT)(2)    /* Illegal value read. */
  48. #define    RESULT_OVERFLOW        (MIDI_RESULT)(3)    /* Value overflowed.   */
  49. #define    RESULT_STOP        (MIDI_RESULT)(4)    /* End of data.        */
  50.     
  51. /**************************************************************************
  52. * States of our finite automaton for reading input.
  53. **************************************************************************/
  54.     
  55. #define    STATE_SUCCESS        1    /* Got a legal value. */
  56. #define    STATE_ERROR        2    /* Had an input error. */
  57. #define    STATE_OVERFLOW        3    /* Read too big a value. */
  58. #define    STATE_NORMAL        4    /* Haven't read anything yet. */
  59. #define    STATE_INCOMMENT        5    /* Skipping a comment. */
  60. #define    STATE_INDECIMAL        6    /* Reading a base 10 number. */
  61. #define    STATE_LEADZERO        7    /* Read a leading zero. */
  62. #define    STATE_INOCTAL        8    /* Reading an octal number. */
  63. #define    STATE_STARTHEX        9    /* Read 1st digit of hex number. */
  64. #define    STATE_INHEX        10    /* Reading a hex number. */
  65. #define    STATE_STARTBINARY    11    /* Read 1st symbol of binary num. */
  66. #define    STATE_INBINARY        12    /* Reading a binary number. */
  67. #define    STATE_NEGATIVE        13    /* UNUSED.  FOR FUTURE UPDATE. */
  68. #define    STATE_INCHAR        14    /* Reading a char constant. */
  69. #define    STATE_EXPECTQUOTE    15    /* Finishing a char constant. */
  70. #define    STATE_BACKSLASHINCHAR    16    /* Backslash in a char constant. */
  71. #define    STATE_BACKSLASHINSTRING    17    /* Backslash in a string constant. */
  72. #define    STATE_INCLUDEFILE    18    /* UNUSED.  FOR FUTURE UPDATE. */
  73.  
  74.     
  75. /**************************************************************************
  76. * Macros for converting characters to numbers:
  77. *
  78. *    D_TO_INT(c)    Convert a digit to its numeric value.
  79. *    L_TO_INT(c)    Convert a letter (A..F) to its base 16 value.
  80. *    H_TO_INT(c)    Convert a hex digit to its numeric value.
  81. **************************************************************************/
  82.     
  83. #define    D_TO_INT(c)    ((long)((c) - '0'))
  84. #define    L_TO_INT(c)    ((long)((toupper(c)) - 'A') + 10)
  85. #define    H_TO_INT(c)    (isdigit(c) ? D_TO_INT(c) : L_TO_INT(c))
  86.     
  87. /**************************************************************************
  88. * Macros for classifying types of input.  The rest are in <ctype.h>.
  89. **************************************************************************/
  90.     
  91. #define    isoctal(c)    (((c) >= '0') && ((c) <= '7'))
  92.  
  93. /**************************************************************************
  94. * Other macros.
  95. **************************************************************************/
  96.  
  97. #define    MAX(a,b)    (((a) > (b)) ? (a) : (b))
  98. #define    MIN(a,b)    (((a) < (b)) ? (a) : (b))
  99.  
  100. #define OpenReadFile(filename)    OpenAFile(filename, "r", "read")
  101. #define OpenWriteFile(filename)    OpenAFile(filename, "w", "write")
  102.  
  103.     
  104. /**************************************************************************
  105. * The largest legal value our input routine can handle.
  106. **************************************************************************/
  107.     
  108. #define    LARGEST_VALUE        (MIDI_VALUE)(255)
  109. #define    BITS_IN_MIDI_VALUE    BITSPERBYTE        /* in types.h */
  110.  
  111. /***************************************************************************
  112. * Character constants for classifying the input.
  113. ***************************************************************************/
  114.  
  115. #define    START_BINARY    '#'    /* Symbol for start of binary number. */
  116. #define    HELP_SYMBOL    '?'    /* Symbol for help request from user. */
  117. #define    START_COMMENT    ';'    /* Start-comment symbol. */
  118.     
  119. /***************************************************************************
  120. * Command-line Options.
  121. ***************************************************************************/
  122.  
  123. #define    OPT_INPUT    'i'        /* Input format. */
  124. #define    OPT_OUTPUT    'o'        /* Output format. */
  125. #define    OPT_TEXT    't'        /* Text I/O. */
  126. #define    OPT_BINARY    'b'        /* Binary I/O. */
  127. #define    OPT_MIDI    'm'        /* MIDI I/O. */
  128. #define    OPT_SYSEX    'x'        /* UNUSED. FOR FUTURE UPDATE. */
  129. #define    OPT_INFILE    'g'        /* Get from file. */
  130. #define    OPT_OUTFILE    'p'        /* Put to file. */
  131.  
  132. /* For getopt(). */
  133. #define    GETOPT_OPTIONS    "i:o:g:p:x";
  134. extern int optind;
  135. extern char *optarg;
  136.  
  137. /***************************************************************************
  138. * Flags, and their associated macros.
  139. ***************************************************************************/
  140.  
  141. #define    NUM_FLAGS    3    /* How many flags? */
  142.  
  143. #define    FLAG_ITYPE    0    /* Flag ID's. */
  144. #define    FLAG_OTYPE    1
  145. #define    FLAG_SYSEX    2
  146.  
  147.     
  148. /***************************************************************************
  149. * Prototypes.
  150. ***************************************************************************/
  151.  
  152. /* read.c */
  153. STATE_T NewState(int c, STATE_T state, MIDI_VALUE *answer);
  154. STATE_T NonStringState(int c, STATE_T state, MIDI_VALUE *answer);
  155. STATE_T DoNormal(int c, MIDI_VALUE *answer);
  156. STATE_T DoDecimal(int c, MIDI_VALUE *answer);
  157. STATE_T DoOctal(int c, MIDI_VALUE *answer);
  158. STATE_T DoHex(int c, MIDI_VALUE *answer);
  159. STATE_T DoInHex(int c, MIDI_VALUE *answer);
  160. STATE_T DoBinary(int c, MIDI_VALUE *answer);
  161. STATE_T DoInBinary(int c, MIDI_VALUE *answer);
  162. STATE_T DoLeadZero(int c, MIDI_VALUE *answer);
  163. STATE_T IncreaseIfPossible(MIDI_VALUE *answer, int newNum, int base, 
  164.                 STATE_T newState);
  165. STATE_T DoInChar(int c, MIDI_VALUE *answer);
  166. STATE_T DoExpectQuote(int c, MIDI_VALUE *answer);
  167. STATE_T DoBackslash(int c, MIDI_VALUE *answer, STATE_T newState);
  168. STATE_T DoInString(int c, STATE_T state, MIDI_VALUE *answer,
  169.              short *inString);
  170. STATE_T DoComment(int c);
  171. void InputHelp();
  172.  
  173. void PrintNumber(MIDI_VALUE number, FILE *out);
  174. void PrintBinary(MIDI_VALUE number, FILE *out);
  175.  
  176. /* serial.c */
  177. short SerialSetup(FLAGS sysex);
  178. void SerialShutdown(void);
  179. void ResetSerialPort(void);
  180. long AnyMidiData(void);
  181. void PrepareToReadMidi(UBYTE *buf, int len);
  182. void PrepareToWriteMidi(UBYTE *buf, int len);
  183. long DoTheIO(void);
  184. long FastSerialRead(UBYTE buf[]);
  185.  
  186. /* iofunctions.c */
  187. MIDI_RESULT FromMidi(FILE *in, MIDI_VALUE *value);
  188. BOOL ToMidi(FILE *in, MIDI_VALUE value);
  189. MIDI_RESULT FromText(FILE *in, MIDI_VALUE *value);
  190. BOOL ToText(FILE *in, MIDI_VALUE value);
  191. MIDI_RESULT FromBinary(FILE *in, MIDI_VALUE *value);
  192. BOOL ToBinary(FILE *in, MIDI_VALUE value);
  193. void SkipText(FILE *in, MIDI_VALUE value);
  194. void SkipMidi(FILE *in, MIDI_VALUE junk);
  195. void SkipBinary(FILE *in, MIDI_VALUE junk);
  196.  
  197. /* main.c */
  198. BOOL IsIOType(char ioType);
  199. BOOL CheckIOType(char opt, int ioFlag, FLAGS theFlags[]);
  200. BOOL HandleOptions(int argc, char *argv[], FLAGS theFlags[],
  201.            char **infile, char **outfile);
  202. void ReadAndWriteStuff(FLAGS theFlags[], FILE *in, FILE *out);
  203. void SetTheFunctions(MIDI_RESULT (**getfcn)(), BOOL (**putfcn)(),
  204.             void (**skipfcn)(), FLAGS theFlags[]);
  205. BOOL CheckFlags(FLAGS theFlags[]);
  206. void InitStuff(FLAGS theFlags[], FILE **in, FILE **out, char **inFile,
  207.         char **outFile);
  208. BOOL MIDIPlayground(FLAGS theFlags[], FILE *in, FILE *out);
  209.  
  210. /* wb.c */
  211.  
  212. /* help.c */
  213. void BegForUsage(char *progName);
  214. void Help(char *progName);
  215.  
  216. /* files.c */
  217. FILE *OpenAFile(char *filename, char *mode, char *readOrWrite);
  218. BOOL FileExists(char *filename);
  219. BOOL DontOverwriteExistingFile(char *filename);
  220. BOOL SetupFiles(char *infile, char *outfile, FILE **in, FILE **out);
  221. void CloseFiles(FILE *in, FILE *out, char *filein, char *fileout);
  222. BOOL MakeFilename(char **dest, char *src);
  223.  
  224. #ifndef OpenReadFile
  225. FILE *OpenReadFile(char *filename);
  226. #endif
  227.  
  228. #ifndef OpenWriteFile
  229. FILE *OpenWriteFile(char *filename);
  230. #endif
  231.  
  232.  
  233. #endif /* _MP_H */
  234.